package net.ib.mtalk.ui.chat;

import java.io.FileNotFoundException;
import java.io.IOException;

import net.ib.mtalk.R;
import net.ib.mtalk.database.helper.InboxDatabaseHelper;
import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.util.GpbSupport;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;

public class ChatListBackgroundActivity extends Activity
{
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 없는 뷰이면 실행도 하지 않는다.
		Bundle extra = getIntent().getExtras();
		if(InboxDatabaseHelper.getViewUUID(this, extra.getLong(ChatListActivity.INTENT_CLIENT_VIEW_MSB), extra.getLong(ChatListActivity.INTENT_CLIENT_VIEW_LSB)) == null
				|| !InboxDatabaseHelper.getViewUUID(this, extra.getLong(ChatListActivity.INTENT_CLIENT_VIEW_MSB), extra.getLong(ChatListActivity.INTENT_CLIENT_VIEW_LSB)).hasMsb()
				|| !InboxDatabaseHelper.getViewUUID(this, extra.getLong(ChatListActivity.INTENT_CLIENT_VIEW_MSB), extra.getLong(ChatListActivity.INTENT_CLIENT_VIEW_LSB)).hasLsb()
				|| !InboxDatabaseHelper.getViewUUID(this, extra.getLong(ChatListActivity.INTENT_CLIENT_VIEW_MSB), extra.getLong(ChatListActivity.INTENT_CLIENT_VIEW_LSB)).isInitialized()){
			finish();
			return;
		}

		setContentView(R.layout.activity_chatlist_background);
		ImageView iv = (ImageView) findViewById(R.id.chatlist_bg);
		String uri = GlobalPreferences.getBgImageUriString();
		int color = GlobalPreferences.getBgColor();
		if(uri != null)
		{
			try {
				iv.setImageBitmap(MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.parse(uri)));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else if(color != -1)
		{
			iv.setImageDrawable(new ColorDrawable(color));
		}
		else
		{
			color = getResources().getColor(R.color.chat_background_default);
			iv.setImageDrawable(new ColorDrawable(color));
		}
		
		startActivityForResult(getIntent().setClass(this, ChatListActivity.class).setFlags(0), 1);
		//overridePendingTransition(R.anim.slide_bottom_in, R.anim.slide_bottom_out);
	}
	
	public void onResume()
	{
		super.onResume();
		if(GlobalPreferences.getFixOrientation(this))
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		else
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		finish();
		//overridePendingTransition(R.anim.slide_bottom_in, R.anim.slide_bottom_out);
	}
}
